<template>
  <view class="home pageBg">
    <custom-nav-bar title="壁纸"></custom-nav-bar>
    <view class="banner">
      <swiper
        class="swiper"
        indicator-dots
        indicator-color="rgba(255,255,255,0.5)"
        indicator-active-color="#fff"
        autoplay
        circular
      >
        <swiper-item
          class="swiper-item"
          v-for="banner in bannerList"
          :key="banner._id"
        >
          <image
            @click="onNavigator(banner)"
            class="image"
            :src="banner.picurl"
            mode="aspectFill"
          />
        </swiper-item>
      </swiper>
    </view>

    <view class="notice">
      <view class="left">
        <uni-icons
          type="sound-filled"
          size="20"
          color="var(--uni-text-1)"
        ></uni-icons>
        <text class="text">公告</text>
      </view>
      <view class="center">
        <swiper class="swiper" autoplay circular vertical duration="300">
          <swiper-item
            class="swiper-item"
            v-for="notice in noticeList"
            :key="notice._id"
          >
            <navigator :url="`/pages/notice/detail?id=${notice._id}`">{{
              notice.title
            }}</navigator>
          </swiper-item>
        </swiper>
      </view>
      <view class="right">
        <uni-icons type="right" size="16" color="#333"></uni-icons>
      </view>
    </view>

    <view class="select">
      <common-title>
        <template #name>每日推荐</template>
        <template #custom>
          <view class="date">
            <uni-icons
              type="calendar-filled"
              size="18"
              color="var(--uni-text-1)"
            ></uni-icons>
            <uni-dateformat
              :date="Date.now()"
              format="dd"
              class="date-text"
            ></uni-dateformat>
          </view>
        </template>
      </common-title>
      <view class="content">
        <scroll-view class="scroll-view" scroll-x>
          <view
            class="box"
            v-for="random in randomList"
            :key="random._id"
            @click="goPreview(random._id)"
          >
            <image class="image" :src="random.smallPicurl" mode="aspectFill" />
          </view>
        </scroll-view>
      </view>
    </view>
    <view class="theme">
      <common-title>
        <template #name>专题精选</template>
        <template #custom>
          <navigator url="" class="more"> 更多+ </navigator>
        </template>
      </common-title>
      <view class="content">
        <theme-item
          v-for="classify in classifyList"
          :key="classify._id"
          :name="classify.name"
          :picurl="classify.picurl"
          :updateTime="classify.updateTime"
          :classifyId="classify._id"
          observeShow
        ></theme-item>
        <theme-item key="isMore" :isMore="true"></theme-item>
      </view>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from "vue";
import { request } from "@/utils/request";
import type { Banner, News, Random } from "@/api/home";
import type { Classify } from "@/api/classify";
import { apiGetBanner, apiGetRandomWall, apiGetWallNewsList } from "@/api/home";
import { apiGetClassify } from "@/api/classify";
import { onShareAppMessage, onShareTimeline, onReady } from "@dcloudio/uni-app";
import { useClassifyListStore } from "@/stores/classifyList";
const store = useClassifyListStore();
// 作为 action 的 increment 可以直接解构
const { setList } = store;

const bannerList = ref<Banner[]>([]);
const randomList = ref<Random[]>([]);
const noticeList = ref<News[]>([]);
const classifyList = ref<Classify[]>([]);
const goPreview = (id: string) => {
  uni.navigateTo({
    url: `/pages/preview/preview?id=${id}`,
  });
};
const onNavigator = (banner: Banner) => {
  if (banner.target === "self") {
    return uni.navigateTo({
      url: `/pages/classifyList/classifyList?${banner.url}`,
    });
  }

  // #ifndef H5
  uni.navigateToMiniProgram({
    appId: banner.appid as string,
    path: banner.path,
  });
  // #endif
};
const getBanner = async () => {
  const res = await apiGetBanner();
  bannerList.value = res.data;
};
const getRandomWall = async () => {
  const res = await apiGetRandomWall();
  randomList.value = res.data;
  setList(randomList.value);
};
const getWallNewsList = async () => {
  const res = await apiGetWallNewsList({
    select: true,
  });
  noticeList.value = res.data;
};

const getClassify = async () => {
  const res = await apiGetClassify({
    select: true,
  });

  classifyList.value = res.data;
};
onShareAppMessage(() => {
  return {
    title: "壁纸分享",
    path: "pages/index/index",
  };
});
// onShareTimeline(() => {
//   return {
//     title: '壁纸分享',
//   }
// })

getBanner();
getRandomWall();
getWallNewsList();
getClassify();
</script>

<style lang="scss" scoped>
.home {
  .banner {
    width: 750rpx;
    padding: 30rpx 0;

    .swiper {
      width: 750rpx;
      height: 340rpx;

      &-item {
        width: 100%;
        height: 100%;
        padding: 0 30rpx;

        .image {
          width: 100%;
          height: 100%;
          border-radius: 10rpx;
        }
      }
    }
  }

  .notice {
    width: 690rpx;
    height: 80rpx;
    line-height: 80rpx;
    background-color: #f9f9f9;
    margin: 0 auto;
    display: flex;

    .left {
      width: 140rpx;
      display: flex;
      align-items: center;
      justify-content: center;

      .text {
        color: var(--uni-text-1);
        font-weight: 600;
        font-size: 28rpx;
      }
    }

    .center {
      flex: 1;

      .swiper {
        height: 100%;

        &-item {
          height: 100%;
          font-size: 28rpx;
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
        }
      }
    }

    .right {
      width: 70rpx;
      display: flex;
      align-items: center;
      justify-content: center;
    }
  }

  .select {
    padding-top: 50rpx;

    .date {
      color: var(--uni-text-1);

      &-text {
        margin-left: 5rpx;
      }
    }

    .content {
      width: 720rpx;
      margin-left: 30rpx;
      margin-top: 30rpx;

      .scroll-view {
        white-space: nowrap;

        .box {
          width: 200rpx;
          height: 430rpx;
          display: inline-block;
          margin-right: 15rpx;

          .image {
            width: 100%;
            height: 100%;
          }

          &:last-child {
            margin-right: 30rpx;
          }
        }
      }
    }
  }

  .theme {
    padding-top: 50rpx;
    padding-bottom: calc(env(safe-area-inset-bottom) + 50rpx);

    .more {
      font-size: 32rpx;
      color: #888;
    }

    .content {
      margin-top: 30rpx;
      padding: 0 30rpx;
      display: grid;
      gap: 15rpx;
      grid-template-columns: repeat(3, 1fr);
    }
  }
}
</style>
